package sys;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;

/**
 *
 * @author Jonas
 */
public class VideoListModel extends DefaultListModel implements DownloadProgressListener {

    private static List<Video> videoList;
    private DecimalFormat nf;

    public VideoListModel() {
        super();
        videoList = new ArrayList<Video>();
        this.nf = new DecimalFormat("000");
    }

    public void addVideo(Video video) {
        if(!videoList.contains(video)) {
            videoList.add(video);
            video.addDownloadProgressListener(this);
            int index0 = videoList.size()-1;
            int index1 = index0;
            this.fireIntervalAdded(this, index0, index1);
        }
    }
    
    public void addVideos(List<Video> videos) {
    	for(Video v : videos) {
    		addVideo(v);
    	}
    }
    
    public void removeVideo(Video video) {
        if(videoList.contains(video)) {
        	int index0, index1;
        	index0 = 0;
        	
            for (int i = 0; i < videoList.size(); i++) {
            	if(video == videoList.get(i)) {
            		index0 = i;
            	}
			}
            
        	videoList.remove(video);
            
            index1 = index0;
            this.fireIntervalRemoved(this, index0, index1);
        }
    }

    public void removeVideoAtIndexes(int[] indexes) {
        for (int i = 0; i < indexes.length; i++) {
			removeVideo(videoList.get(i));
		}
        this.fireIntervalRemoved(this, 0, videoList.size());
    }

    public Video getVideo(Video video) {
        return videoList.get(videoList.indexOf(video));
    }

    public Video getVideo(int index) {
        return videoList.get(index);
    }

    public List<Video> getVideos() {
        return videoList;
    }

    public synchronized void updateDownloadProgress(Video video) {
        if(videoList.indexOf(video) != -1) {
        	int index0, index1;
        	index0 = videoList.indexOf(video);
        	index1 = index0;
            this.fireContentsChanged(this, index0, index1);
        }
    }

	@Override
	public Object getElementAt(int arg0) {
		
		return videoList.get(arg0);
	}

	@Override
	public int getSize() {
		return videoList.size();
	}

}
